﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ID3TagLib;
using System.Xml.Serialization;
using System.Runtime.InteropServices;
using System.Xml;
using System.Data;
using Microsoft.WindowsAPICodePack.Shell;

namespace Home.ArtDB
{
    /// <summary>
    /// 
    /// </summary>
    public class ArtFile
    {
        #region CONSTANTE

        public const string ARTIST = "Artist";
        public const string COMPOSER = "Author";
        public const string TRACK = "Track";
        public const string TITLE = "Title";
        public const string ALBUM = "Album";
        public const string COMMENT = "Comment";
        public const string GENRE = "Genre";
        public const string FILENAME = "FileName";
        public const string SIZE = "Size";
        public const string HOMEID = "HomeID";
        public const string LANGUAGE = "Language";
        public const string DURATION = "DURATION";
        public const string YEAR = "Year";
        public const string FILESCOUNT = "Files";

        #endregion constante

        /// <summary></summary>
        protected ArtFileCollection _parentCollection;

        /// <summary></summary>
        protected DataRow _row;

        internal DataRow Row { get { return _row; } }

        /// <summary></summary>
        public bool Valid = false;

        #region PROPERTIES

        /// <summary></summary>
        public string FileName
        {
            get { return _row[FILENAME].ToString(); }
            set { _row[FILENAME] = value; }
        }
        /// <summary></summary>
        public string Title
        {
            get { return _row[TITLE].ToString(); }
            set { _row[TITLE] = value; }
        }
        /// <summary></summary>
        public string Composer
        {
            get { return _row[COMPOSER].ToString(); }
            set { _row[COMPOSER] = value; }
        }
        /// <summary></summary>
        public string Artist
        {
            get { return _row[ARTIST].ToString(); }
            set { _row[ARTIST] = value; }
        }
        /// <summary></summary>
        public string Album
        {
            get { return _row[ALBUM].ToString(); }
            set { _row[ALBUM] = value; }
        }
        /// <summary></summary>
        public string Year
        {
            get { return _row[YEAR].ToString(); }
            set { _row[YEAR] = value; }
        }
        /// <summary></summary>
        public string Comment
        {
            get { return _row[COMMENT].ToString(); }
            set { _row[COMMENT] = value; }
        }
        /// <summary></summary>
        public int Track
        {
            get { return _row[TRACK].ToInt(); }
            set { _row[TRACK] = value; }
        }
        /// <summary></summary>
        public string Language
        {
            get { return _row[LANGUAGE].ToString(); }
            set { _row[LANGUAGE] = value; }
        }
        /// <summary></summary>
        public string Genre
        {
            get { return _row[GENRE].ToString(); }
            set { _row[GENRE] = value; }
        }
        /// <summary></summary>
        public ulong Duration
        {
            get { return _row[DURATION].ToULong(); }
            set { _row[DURATION] = value; }
        }
        /// <summary></summary>
        public string HomeID
        {
            get { return _row[HOMEID].ToString(); }
            set { _row[HOMEID] = value; }
        }

        /// <summary></summary>
        public long Size
        {
            get { return _row[SIZE].ToLong(); }
            set { _row[SIZE] = value; }
        }
        #endregion properties

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentCollection"></param>
        /// <param name="filename"></param>
        internal ArtFile(ArtFileCollection parentCollection, string filename)
        {
            _parentCollection = parentCollection;

            _row = _parentCollection.Table.NewRow();

            Load(filename);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentCollection"></param>
        /// <param name="row"></param>
        internal ArtFile(ArtFileCollection parentCollection, DataRow row)
        {
            _parentCollection = parentCollection;
            _row = row;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        internal static ArtFile Create(ArtFileCollection parentCollection, string filename)
        {
            ArtFile artFile = null;

            if (Path.GetExtension(filename).Equals(".mp3", StringComparison.OrdinalIgnoreCase) ||
                Path.GetExtension(filename).Equals(".wav", StringComparison.OrdinalIgnoreCase))
            {
                artFile = new ArtAudioFile(parentCollection, filename);
            }
            else if (Path.GetExtension(filename).Equals(".epub", StringComparison.OrdinalIgnoreCase))
            {
                artFile = new ArtEpubFile(parentCollection, filename);
            }

            return artFile;
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual void Save()        {        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public virtual void Load(string filename)
        {
            FileName = filename;
           
        }

    }

}
